﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Net;
using System.IO;
using System.Text.RegularExpressions;
using DotNet_Common;

namespace res.Models
{
    public class BaiduGet
    {
        private HttpWebRequest request;
        private WebResponse response;
        public string recount = "0";


        //获得页面源代码
        public string GetPageText(string url, CookieContainer cookie)
        {
            //MessageBox.Show("尝试获取页面");
            string re = "";

            request = (HttpWebRequest)WebRequest.Create(url + "#getter=" + new Random().ToString());
            request.Timeout = 20000;
            request.CookieContainer = cookie;
            request.Accept = "*/*";
            request.UserAgent = "Mozilla/4.0   (compatible;   MSIE   6.0;   Windows   NT   5.1;   SV1;   .NET   CLR   1.1.4322) ";
            request.ContentType = "application/x-www-form-urlencoded";
            request.AllowAutoRedirect = true;

            Stream resStream = null;
            StreamReader sr = null;
            try
            {
                response = request.GetResponse();
                resStream = response.GetResponseStream();
                sr = new StreamReader(resStream, System.Text.Encoding.Default);
                re = sr.ReadToEnd();
            }
            catch
            {
                re = "geterror";
            }
            finally
            {
                if (resStream != null)
                {
                    resStream.Close();
                }
                if (sr != null)
                {
                    sr.Close();
                    sr.Dispose();
                }

            }

            return re;
        }

        public SeResultList GetSeResult(string url, string cat)
        {
            string pagetxt = GetPageText(url,null);

            //处理页码
            string p = StrCommon.GetStr(pagetxt, "<div class=\"p\">", "</div>");
            p = Regex.Replace(p, "s?lm=0(.*?)pn=([0-9]+)(.*?)usm=0", "$2");

            //获得总结果数
            recount = StrCommon.GetStr(pagetxt, "找到相关网页约", "篇");
            if (recount == "")
            {
                recount = "0";
            }


            //处理内容
            string regex = @"<table.*?id=""[0-9]+"">.*?</table>";

            MatchCollection result = Regex.Matches(pagetxt, regex);

            SeResultList re2 = new SeResultList("", null);

            if (result.Count > 0)
            {
                SeResult[] re = new SeResult[result.Count];

                int i = 0;

                foreach (Match t in result)
                {
                    re[i] = new SeResult(t.Value.ToString(), cat);
                    i = i + 1;
                }

                re2 = new SeResultList(p, re);
            }

            return re2;
        }
    }


    public class SeResult
    {
        public string title {
            get { return _title; }
        }
        public string url
        {
            get { return _url; }
        }
        public string intro
        {
            get { return _intro; }
        }

        string _title = "";
        string _url = "";
        string _intro = "";

        public SeResult(string tabletxt,string cat)
        {
            //HttpContext.Current.Response.Write(tabletxt+"<br />-----<br />");
            if (cat == "doc")
            {
                _title = StrCommon.GetStr(tabletxt, "target=\"_blank\">", "</a>");
                _url = StrCommon.GetStr(tabletxt, ")\" href=\"", "\" target=\"_blank\"");
                _intro = StrCommon.GetStr(tabletxt, "HTML版</a><br>", "<br>");
            }
            else if (cat == "law")
            {
                _url = StrCommon.GetStr(tabletxt, "href=\"", "\" target=\"_blank\">");
                _title = StrCommon.GetStr(tabletxt, "_blank\"><font size=\"3\">", "</font></a><br>");
                _title = _title.Replace("_百度法律", "");
                _intro = StrCommon.GetStr(tabletxt, "<font size=-1>", "<br><font color=#008000>");
            }
            else if (cat == "zhuanli")
            {
                _url = StrCommon.GetStr(tabletxt, "href=\"", "\" target=\"_blank\">");
                _title = StrCommon.GetStr(tabletxt, "_blank\"><font size=\"3\">", "_百度专利搜索");
                _intro = StrCommon.GetStr(tabletxt, "<font size=-1>", "<br><font color=#008000>");
            }
        }
    }


    public class SeResultList
    {
        public SeResult[] list
        {
            get { return _list; }
        }

        public string pagetext
        {
            get { return _pagetext; }
        }

        string _pagetext;
        SeResult[] _list;

        public SeResultList(string p,SeResult[] l)
        {
            _pagetext = p;
            _list = l;
        }


    }
}
